<?php

namespace App\Http\Controllers\product;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;
use Intervention\Image\ImageManagerStatic as Image;
class ProductController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        //保存搜索条件
        $where = [];
        //实例化要操作的表
        $db = DB::table('product')
        ->join('type','product.path_id','=','type.id')
        ->select('product.*', 'type.name as typename');
        
        
        // dd($db);
        //判断有没有搜索name字段的值
        // dd();
        if($request->has('name')){
        //     // 获取要搜索的name字段的值
            $name = $request->input('name');
        //     //添加where模糊匹配条件
            $db->where('product.name','like',"%{$name}%")->orderBy('product.path_id');
            $where['name'] = $name;
        }
        // 分页
        $list = $db->paginate(10);
        // dd($list);
        // $list = $db->get();
        //加载模板
        return view('product.index',['list'=>$list,'where'=>$where]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {	$pro=DB::table('type')->orderBy('path')->get()->all();
    	

    	
    	
    	
    	

        return view('product.add',['pro'=>$pro]);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
	{
        // dd($request);

         // 自动规则
        $messages = [
            'required' => ':attribute 是必填的',
        ];
        //
        $this->validate($request, [
            'name' => 'required|unique:product|max:16',
            
            'picture'=>'required',
            'big_picture'=>'required',
            'path_id'=>'required',
            'fixed_price'=>'required',
            'lower_price'=>'required'
            
        ],$messages);
        $data = $request->except('_token');
        $file1=$request->file('picture');
        $file2=$request->file('big_picture');
         $ext1 = $file1->getClientOriginalExtension();
		 $picname1 = time().rand(1000,9999).'.'.$ext1;
		 $ext2 = $file2->getClientOriginalExtension();
		 $picname2 = time().rand(1000,9999).'.'.$ext2;
		 // dd($data['picture']);
		 // dd($picname1);
		$data['picture']=$picname1;
		 // dd($data);
		$data['big_picture']=$picname2;
        $id = DB::table('product')->insertGetId($data);
        if($id>0){
      //   	if ($request->hasFile('picture')) {
    		// if ($request->file('picture')->isValid()) {
			   //  $file = $request->file('picture');
			   //  dd($file);
			    // $ext1 = $file1->getClientOriginalExtension();
			    // $picname = time().rand(1000,9999).'.'.$ext1;
			    $file1->move('./images/',$picname1);
			    //  $ext2 = $file2->getClientOriginalExtension();
			    // $picname = time().rand(1000,9999).'.'.$ext2;
			    $file2->move('./images/',$picname2);

			    
		// 	}
		// }
            return redirect('/admin/product')->with('msg','添加成功');
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $product= DB::table('product')->where('id',$id)->get()->all();
        $type=DB::table('type')->orderBy('path')->get()->all();
        // dd($product);
    	return view('product.edit',['product'=>$product,'type'=>$type,'id'=>$id]);
        
        
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {		
    	 $messages = [
            'required' => ':attribute 是必填的',
        ];
        //
        $this->validate($request, [
            'name' => 'required|max:16',
            'keywords'=>'required',
            
            'path_id'=>'required',
            'fixed_price'=>'required',
            'lower_price'=>'required',
            
        ],$messages);
        $data = $request->except('_token','_method');
        if($request->has('file')){
        $file1=$request->file('picture');
        $file2=$request->file('big_picture');
         $ext1 = $file1->getClientOriginalExtension();
		 $picname1 = time().rand(1000,9999).'.'.$ext1;
		 $ext2 = $file2->getClientOriginalExtension();
		 $picname2 = time().rand(1000,9999).'.'.$ext2;
		 // dd($data);
		 $date['picture']=$picname1;
		 $date['big_picture']=$picname2;
		}
        $ac= DB::table('product')->where('id',$id)->update($data);
        if($request->has('file')){
       	if($ac>0){
	     //   	$file1=$request->file('picture');
	     //    $file2=$request->file('big_picture');
   		 //    $ext1 = $file1->getClientOriginalExtension();
		    // $picname = time().rand(1000,9999).'.'.$ext1;
		    $file1->move('./images/',$picname1);
		    // $ext2 = $file2->getClientOriginalExtension();
		    // $picname = time().rand(1000,9999).'.'.$ext2;
		    $file2->move('./images/',$picname2);
	     }
		}
            return redirect('/admin/product')->with('msg','修改成功');
        
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $row = DB::table('product')->where('id',$id)->delete();
        if($row>0){
            return redirect('admin/product')->with('msg','删除成功');
        }
    }
}
